/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.tutor.web.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opgea.tutor.TutorException;
import com.opgea.tutor.domain.model.SimpleModel;
import com.opgea.tutor.domain.qualifiers.UserType;
import com.opgea.tutor.service.LoginService;
import com.opgea.tutor.service.StudentService;
import com.opgea.tutor.service.TutorService;
import com.opgea.tutor.service.impl.ApplicationServiceImpl;
import com.opgea.tutor.util.FacesUtil;
import com.opgea.tutor.web.dto.LoginDTO;
import com.opgea.tutor.web.dto.StudentDTO;
import com.opgea.tutor.web.dto.TutorDTO;

/**
 * 
 * @author Ramesh
 */
@ManagedBean(name = "loginController")
@RequestScoped
@Component
@Scope(value = "request")
public class LoginController {

    private Logger logger = Logger.getLogger(LoginController.class);

    @Autowired
    private LoginService loginService;
    @Autowired
    private TutorService tutorService;
    @Autowired
    private StudentService studentService;

    private LoginDTO loginDTO = new LoginDTO();
    private List<SimpleModel> userTypeList = new ArrayList<SimpleModel>();
    private List<LoginDTO> loginDTOList = new ArrayList<LoginDTO>();

    @PostConstruct
    public void init() {
	loginDTOList = loginService.findAll();
    }

    public LoginDTO getLoginDTO() {
	return loginDTO;
    }

    public void setLoginDTO(LoginDTO loginDTO) {
	this.loginDTO = loginDTO;
    }

    public List<SimpleModel> getUserTypeList() {
	userTypeList.clear();
	for (UserType value : UserType.values()) {
	    userTypeList.add(new SimpleModel(new Long(value.ordinal()), value
		    .toString()));
	}
	return userTypeList;
    }

    public void setUserTypeList(List<SimpleModel> userTypeList) {
	this.userTypeList = userTypeList;
    }

    public List<LoginDTO> getLoginDTOList() {
	return loginDTOList;
    }

    public void setLoginDTOList(List<LoginDTO> loginDTOList) {
	this.loginDTOList = loginDTOList;
    }

    public String save() {
	loginService.create(loginDTO);
	return "success.xhtml";
    }

    public String view() {
	return "view.xhtml";
    }

    public String edit() {
	ExternalContext extContext = FacesContext.getCurrentInstance()
		.getExternalContext();
	Map<String, String> params = extContext.getRequestParameterMap();
	String loginId = (String) params.get("loginId");
	this.loginDTO = loginService.find(new Long(loginId));
	return "create.xhtml";
    }

    public String list() {
	return "list.xhtml";
    }

    public String verify() {
	logger.info("Inside Verify...");

	Map<String, Object> responseMap = loginService.verify(
		loginDTO.getLoginId(), loginDTO.getPassword());
	if (responseMap.get("loginId") == Boolean.TRUE
		&& responseMap.get("password") == Boolean.TRUE) {
	    LoginDTO dto = (LoginDTO) responseMap.get("object");
	    FacesUtil.getSessionUserDataObject().setLoginId(dto.getLoginId());
	    FacesUtil.getSessionUserDataObject().setLoginIdPrimaryKey(
		    dto.getId());
	    FacesUtil.getSessionUserDataObject().setUserType(dto.getUserType());

	    if (FacesUtil.getSessionUserDataObject().getUserType()
		    .equalsIgnoreCase(UserType.Tutor.toString())) {
		TutorDTO tutorDTO = null;
		try {
		    tutorDTO = tutorService.findByLoginId(dto.getId());
		} catch (TutorException e) {
		    FacesUtil.addMessage("messages",
			    FacesMessage.SEVERITY_INFO, "Invalid User Id!",
			    null);
		}
		FacesUtil.getSessionUserDataObject().setPrimaryKey(
			tutorDTO.getId());
		FacesUtil.getSessionUserDataObject().setPageList(
			ApplicationServiceImpl.getPageList(UserType.Tutor));
		FacesUtil.getSessionUserDataObject().setActionList(
			ApplicationServiceImpl.getActionList(1, 1));
	    }
	    if (FacesUtil.getSessionUserDataObject().getUserType()
		    .equalsIgnoreCase(UserType.Student.toString())) {
		StudentDTO studentDTO = studentService.find(dto.getId());
		FacesUtil.getSessionUserDataObject().setPrimaryKey(
			studentDTO.getId());
		FacesUtil.getSessionUserDataObject().setPageList(
			ApplicationServiceImpl.getPageList(UserType.Student));
		FacesUtil.getSessionUserDataObject().setActionList(
			ApplicationServiceImpl.getActionList(1, 1));
	    }
	    logger.info("View: welcome");
	    return "../welcome/welcome.xhtml";
	}
	if (responseMap.get("loginId") == Boolean.FALSE) {
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_ERROR,
		    "Invalid User Id!", null);
	    return "login.xhtml";
	} else {
	    FacesUtil.addMessage("messages", FacesMessage.SEVERITY_ERROR,
		    "Invalid Password!", null);
	    logger.info("View: login");
	    return "login.xhtml";
	}
    }

    public String logout() {
	FacesUtil.getHttpSession().invalidate();
	return "../../login/login.xhtml";
    }
}
